//
//  JYTTabBarItemView.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/12/14.
//  Copyright © 2016年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTTabBarItemView.h"

#define BUTTON_INDEX_OFFSET 1000

@interface JYTTabBarItemView ()

@end

@implementation JYTTabBarItemView

- (instancetype) initWithtabBarItem:(JYTTabBarItem *)item
{
    self = [super init];
    
    if (self) {
        self.selfIndex = item.index;
        
        UIButton *btn = [[UIButton alloc] init];
        [self addSubview:btn];
        
        UIEdgeInsets bpadding = UIEdgeInsetsMake(0, 0, 0, 0);
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).with.insets(bpadding);
        }];
        
        [btn setBackgroundColor:[UIColor redColor]];
        [btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTag:(self.selfIndex + BUTTON_INDEX_OFFSET)];
        
    
        UIImageView *selectedImageView = [[UIImageView alloc] init];
        [self addSubview:selectedImageView];
        
        UIEdgeInsets hipadding = UIEdgeInsetsMake(0, 0, 0, 0);
        [selectedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).with.insets(hipadding);
        }];
        
        [selectedImageView setImage:[UIImage imageNamed:item.selectedImage]];
        self.selectedImageView = selectedImageView;
        
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        
        UIEdgeInsets inpadding = UIEdgeInsetsMake(0, 0, 0, 0);
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).with.insets(inpadding);
            
        }];
        
        [imageView setImage:[UIImage imageNamed:item.image]];
        
        self.imageView = imageView;
        
        
    }
    
    return self;
}

- (void) setNormalImage
{
    [self bringSubviewToFront:self.imageView];
}

- (void) setHighLightImage
{
    [self bringSubviewToFront:self.selectedImageView];
}

- (void) clickButton:(UIButton *)sender
{
    [self setHighLightImage];
    
    if (self.delegate) {
        NSInteger index = sender.tag - BUTTON_INDEX_OFFSET;
        [self.delegate clickTabBarItem:index];
    }
}


@end
